import { Inject, Controller, Get, Query, Post, Body, UseGuard } from '@midwayjs/decorator';
import { Context } from '@midwayjs/koa';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Role } from 'src/decorator/role.decorator';
import { novellistDTO } from 'src/dto/novel';
import { AdminEntity } from 'src/entity/admin/user';
import { NovelEntity } from 'src/entity/novel/novel';
import { OrderEntity } from 'src/entity/order/orderentity';
import { NovelPartEntity } from 'src/entity/part/part';
import { UserEntity } from 'src/entity/userinfo/user';
// import { AdminMiddleware } from 'src/middleware/adminiogin ';
import { AuthGuard } from '../../guard/auth.guard'
import { NovelService } from 'src/service/novel.service';
import { Repository } from 'typeorm';

@UseGuard(AuthGuard)
@Controller('/admin' )
export class APIController {
  @Inject()
  ctx: Context;

  @Inject()
  NovelService: NovelService;

  @InjectEntityModel(NovelEntity)
  NovelModel: Repository<NovelEntity>;

  @InjectEntityModel(OrderEntity)
  OrderModel: Repository<OrderEntity>;

  @InjectEntityModel(NovelPartEntity)
  NovelPartEntity: Repository<NovelPartEntity>;
  @InjectEntityModel(UserEntity)
  UserModel: Repository<UserEntity>;
  @InjectEntityModel(AdminEntity)
  AdminModel: Repository<AdminEntity>;



  @Get('/getnovel')
  async getnovel(@Query('uid') uid) {
   //   return  'Hello Midwayjs12312312!'
     return  this.NovelService.getNovel({ id:1 })
  }
  @Role(['admin'])
  @Post('/addnovelitem')
  async getnovelitem( @Body() body) {
      console.log("addnovelitem",body);
  
     return  this.NovelService.addtNovelitemNode(body.item)
  }


  @Role(['admin'])
  @Post('/addnovel')
  async addnovelitem( @Body() body) {

     return  this.NovelService.addtNovel(body)  
  }

  @Post('/getnovelandpart')
  async getnovelandpart( @Body() body:novellistDTO) {
      console.log("addnovelitem",body);
     return  this.NovelService.adminPagelist(  body ,  
      { 
        keyWordLikeFields :['name','author','id']  , 
    //  leftJonin:['novel.novels','novels']  ,
        count:['a.partSum', 'a.novels']
        // count:['novel.partSum', 'novel.novels']
    } , this.NovelModel )
  }

  @Post('/orderlist')
  async getorderlist( @Body() body:novellistDTO) {
      console.log("addnovelitem",body);
     return  this.NovelService.adminPagelist(  body ,  
      { 
        keyWordLikeFields :['orderSn','shareUserId','userId']  , 
    //  leftJonin:['a.novel','novel'],  单联表
     leftJonin:[['a.novel','novel'] , ['a.userId','userId']],   //多联表
    } , this.OrderModel )
  }
  @Role(['admin'])
  @Post('/userlist')
  async userlist( @Body() body:novellistDTO) {
      console.log("addnovelitem",body);
     return  this.NovelService.adminPagelist(  body ,  
      { 
        keyWordLikeFields :['id']  , 
    //  leftJonin:['a.novel','novel'],  单联表
    //  leftJonin:[['a.novel','novel'] , ['a.userId','userId']],   //多联表
    } , this.UserModel )
  }
  @Role(['admin'])
  @Post('/gentlist')
  async gentlist( @Body() body:novellistDTO) {
      console.log("addnovelitem",body);
     return  this.NovelService.adminPagelist(  body ,  
      { 
        keyWordLikeFields :['id']  , 
    //  leftJonin:['a.novel','novel'],  单联表
    //  leftJonin:[['a.novel','novel'] , ['a.userId','userId']],   //多联表
    } , this.AdminModel )
  }

// 添加收费状态
@Role(['admin'])
  @Post('/novelpartstatus')
  async novelpartstatus( @Body() body:any) {
      console.log("novelpartstatus",body);
    //  return  this.NovelService.novelpartstatus(body)
   let result    = await  this.NovelPartEntity.createQueryBuilder().update().set({ status: body.status}).where("id = :id", { id: body.id })
     .execute();
      return  {
         code:20000,
         result
      }

  }
// 修改标题或者内容状态
@Role(['admin'])
  @Post('/novelpartcontent')
  async novelpartcontent( @Body() body:NovelPartEntity) {
      console.log("novelpartcontent",body);
    //  return  this.NovelService.novelpartstatus(body)
    let { id , ...obj }   = body
   let result    = await  this.NovelPartEntity.createQueryBuilder().update().set(obj).where("id = :id", { id: id})
     .execute();
      return  {
         code:20000,
         result
      }

  }
@Role(['admin'])
  @Post('/deltenovel')
  async deltemovel( @Body() body:NovelPartEntity) {
      console.log("novelpartcontent",body);
    //  return  this.NovelService.novelpartstatus(body)
    let { id  }   = body
    console.log( "删除",id);
    return   this.NovelService.deltenovel(id)
    

  }
@Role(['admin'])
  @Post('/approveStatus')
  async approveStatus( @Body() body:any) {
      console.log("novelpartcontent",body);
    //  return  this.NovelService.novelpartstatus(body)

    return   this.NovelService.approveStatus(body)
    

  }

@Role(['admin'])
  @Post('/ishot')
  async ishot( @Body() body:any) {
      console.log("hot",body);
    //  return  this.NovelService.novelpartstatus(body)

    return   this.NovelService.ishot(body)
    

  }
@Role(['admin'])
  @Post('/evaluateNum')
  async evaluateNum( @Body() body:any) {
      console.log("hot",body);
    //  return  this.NovelService.novelpartstatus(body)

    return   this.NovelService.evaluateNum(body)
    

  }








  //  总付款 所有订单 金额  待结算 有代理的情况下 并且是已经付款的情况  还是 未结算的情况下
  @Post('/orderlist2')
  async getorderlist2( @Body() body:novellistDTO) {
      console.log("addnovelitem",body);
     return  this.NovelService.adminPagelist2(  body ,  
      { 
        keyWordLikeFields :['orderSn','shareUserId','userId']  , 
    //  leftJonin:['a.novel','novel'],  单联表
     leftJonin:[['a.novel','novel'] , ['a.userId','userId']],   //多联表
    } , this.OrderModel )
  }

  @Post('/partlist')
  async partlist( @Body() body:novellistDTO) {
      console.log("addnovelitem",body);
     return  this.NovelService.adminPagelist(  body ,  
      { 
        keyWordLikeFields :['articleTitle','sort']  , 
         leftJonin:['a.Pid','Pid'],    // 单联表
    //  leftJonin:[['a.novel','novel'] , ['a.userId','userId']],   //多联表
    } , this.NovelPartEntity )
  }


  


} 
